package ua.kiev.tosik.price.modules.search.gui;

import ua.kiev.tosik.price.modules.search.conf.ModuleConfig;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainNavigationBar {

	private JPanel navBar;
	private JButton prevButton;
	private JButton nextButton;
	private JLabel pageLabel;
	private ModuleConfig config;
	private MainPanel mainPanel;

	public MainNavigationBar(MainPanel mainPanel) {
		this.mainPanel = mainPanel;
		config = ModuleConfig.getInstance();
		initComponents();
		initActions();
		updateLocale();
	}

	public JPanel getNavBar() {
		return navBar;
	}

	public JLabel getPageLabel() {
		return pageLabel;
	}

	private void initComponents() {
		navBar = new JPanel();
		navBar.setLayout(new BorderLayout() );
		prevButton = new JButton();
		nextButton = new JButton();
		pageLabel = new JLabel();
		pageLabel.setHorizontalAlignment(SwingConstants.CENTER);
		// pageLabel.setFont( new Font(pageLabel.getFont().getName(), Font.BOLD, pageLabel.getFont().getSize()) );

		JPanel panel = new JPanel();
		panel.setAlignmentX(SwingConstants.LEFT);
		prevButton.setHorizontalAlignment(SwingConstants.LEFT);
		panel.add(prevButton);
		navBar.add(panel, BorderLayout.WEST);
		navBar.add(pageLabel, BorderLayout.CENTER);
		JPanel panel2 = new JPanel();
		panel2.setAlignmentX(SwingConstants.LEFT);
		nextButton.setHorizontalAlignment(SwingConstants.LEFT);
		panel2.add(nextButton);
		navBar.add(panel2, BorderLayout.EAST);
	}

	private void initActions() {
		prevButton.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainPanel.listPrev();
			}
		});
		nextButton.addActionListener( new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainPanel.listNext();
			}
		});
	}

	public void updateLocale() {
		prevButton.setToolTipText(config.get("search.navbar.prevbutton.text"));
		prevButton.setIcon( new ImageIcon(config.get("search.navbar.prevbutton.icon")) );
		nextButton.setToolTipText(config.get("search.navbar.nextbutton.text"));
		nextButton.setIcon( new ImageIcon(config.get("search.navbar.nextbutton.icon")) );
	}

	public void enableButton(boolean enabled) {
		prevButton.setEnabled(enabled);
		nextButton.setEnabled(enabled);
	}

}
